package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.entity.VTmRollMng;
import com.yaj.myfactorysupport.mapper.VTmRollMngMapper;
import com.yaj.myfactorysupport.service.VTmRollMngService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.util.SucessCodeEnum;
import com.yaj.myfactorysupport.vo.VTmRollMngRemarkVo;
import com.yaj.myfactorysupport.vo.VTmRollMngSmtSideVo;
import com.yaj.myfactorysupport.vo.VTmRollMngVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-05-31
 */
@Service
public class VTmRollMngServiceImpl extends ServiceImpl<VTmRollMngMapper, VTmRollMng> implements VTmRollMngService {
    @Resource
    private VTmRollMngMapper vTmRollMngMapper;

    @Override
    public RespBean existPcbId(String pcbId) {
        List<VTmRollMng> vTmRollMngs = vTmRollMngMapper.selectList(new QueryWrapper<VTmRollMng>().lambda().eq(VTmRollMng::getPcbId,pcbId));

        if(vTmRollMngs!=null && vTmRollMngs.size()>0){
            return RespBean.success(SucessCodeEnum.记录查找成功.getMsg());
        }

        return  RespBean.error(ErrorCodeEnum.部品轴ID不存在.getCode(),ErrorCodeEnum.部品轴ID不存在.getMsg());
    }

    @Override
    public RespBean getPartsByPcbId(String pcbId,String smtSide,String mounterType) {
        VTmRollMngVo vTmRollMngVo = vTmRollMngMapper.getPartsByPcbId(pcbId,smtSide,mounterType);

        if(vTmRollMngVo!=null){
            return RespBean.success(vTmRollMngVo);
        }

        return RespBean.error(ErrorCodeEnum.没有找到对应PCBID的部品记录.getCode(),ErrorCodeEnum.没有找到对应PCBID的部品记录.getMsg());
    }

    @Override
    public RespBean getTmRollMngPcbId(String pcbId, String smtSide, String mounterType) {
        VTmRollMng vTmRollMng = vTmRollMngMapper.selectOne(new QueryWrapper<VTmRollMng>().lambda()
                                        .eq(VTmRollMng::getPcbId,pcbId)
                                        .eq(VTmRollMng::getSmtSide,smtSide)
                                        .eq(VTmRollMng::getMounterType,mounterType));

        if(ObjectUtil.isNull(vTmRollMng)){
            return  RespBean.error(ErrorCodeEnum.没有找到对应的PCBID.getCode(),ErrorCodeEnum.没有找到对应的PCBID.getMsg());
        }

        VTmRollMngRemarkVo vTmRollMngRemarkVo = new VTmRollMngRemarkVo();
        BeanUtil.copyProperties(vTmRollMng,vTmRollMngRemarkVo);

        return RespBean.success(vTmRollMngRemarkVo);
    }

    @Override
    public RespBean getSmtSideByPcbId(String pcbId,String mounterType) {
        List<VTmRollMng> vTmRollMngs = vTmRollMngMapper.selectList(new QueryWrapper<VTmRollMng>().lambda()
                                                                    .eq(VTmRollMng::getPcbId,pcbId)
                                                                    .eq(VTmRollMng::getMounterType,mounterType));
        List<VTmRollMngSmtSideVo> vTmRollMngSmtSideVos = new ArrayList<VTmRollMngSmtSideVo>();

        if(ObjectUtil.isNull(vTmRollMngs)){
            return  RespBean.error(ErrorCodeEnum.没有找到对应的PCBID.getCode(),ErrorCodeEnum.没有找到对应的PCBID.getMsg());
        }

        for(VTmRollMng vTmRollMng:vTmRollMngs){
            VTmRollMngSmtSideVo vTmRollMngSmtSideVo = new VTmRollMngSmtSideVo();
            BeanUtil.copyProperties(vTmRollMng,vTmRollMngSmtSideVo);
            vTmRollMngSmtSideVos.add(vTmRollMngSmtSideVo);
        }
        vTmRollMngSmtSideVos.sort(Comparator.comparing(VTmRollMngSmtSideVo::getSmtSide));
        return RespBean.success(vTmRollMngSmtSideVos);
    }
}
